package com.example.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.domain.Student;
import com.example.demo.service.StudentService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RequestMapping("/Student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<Student> students =studentService.list();
        model.addAttribute("students", students);
        return "listStudent";
    }

    //删除学生
    @RequestMapping("remove")
    public String remove(int id) {
        studentService.removeById(id);
        return "redirect:/Student/list";
    }
    //分页显示
    @RequestMapping("list/{currPage}/{pageSize}")
    public String list(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize) {
        if(currPage<1) currPage=1;
        if(pageSize <= 0) pageSize=5;
//        List<Student> students=studentService.findByPage(currPage,pageSize);
        Page<Student> page = new Page<>(currPage,pageSize);
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        List<Student> students = studentService.page(page, wrapper).getRecords();
        model.addAttribute("students", students);
        return "listStudent";
    }
}

